package org.aplombh.java.leetcode.all;

public class _24两两交换链表中的节点 {
}

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution24_1 {
    public ListNode swapPairs(ListNode head) {

        ListNode h = new ListNode();

        ListNode l = h;
        ListNode r = head;
        ListNode p;

        l.next = head;

        while (r.next != null) {

            p = r.next;

            l.next = r.next;
            r.next = p.next;
            p.next = r;


            l = r;
            r = r.next;

        }
        return h.next;
    }
}